package com.itshops.dao.impl;

import com.itshops.dao.UserDao;
import com.itshops.entity.User;
import com.itshops.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public User selectUserByName(String username) throws SQLException {
        QueryRunner runner = new QueryRunner(C3P0Utils.getDataSource());
        String  sql = "select u_id as uid , u_name as username , u_password as upassword" +
                ", u_sex as usex ,u_email as email " +
                ", u_role as urole from user where u_name = ?";
        User query=null;
         query= runner.query(sql, new BeanHandler<User>(User.class),username);
        return query;
    }


    @Override
    public int insertUser(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "insert into user (u_name,u_password,u_sex," +
                "u_email,u_role) value (?,?,?,?,?)";
        int i = queryRunner.update(sql, user.getUsername(), user.getUpassword(), user.getUsex(),
                 user.getEmail(), user.getUrole());
        return i;
    }

}
